<?php


namespace app\app\controller;


use think\Db;

class Driver extends WxUser
{
    protected $DriverInfo;
    protected $DriverId;

    protected function initialize()
    {
        parent::initialize();
        $driver_info = Db::name('driver')->where([
            'member_id' => $this->memberId,
            'is_del' => 0
        ])->find();
        if (!$driver_info) {
            responseJson(-100, '司机信息不存在');
        }
        if ($driver_info['status'] != 1) {
            responseJson(-200, '状态异常，请联系管理员');
        }
        $this->DriverInfo = $driver_info;
        $this->DriverId = $driver_info['id'];
        $this->DriverInfo['count']=number_format((100-$driver_info['discount'])/100,2);
    }

    //接送订单员 0下线 1上线
    public function BusinessOnline()
    {
        $data = $this->request->param();
        $update = array(
            'id' => $this->DriverId,
            'is_online' => $data['is_online']
        );
        Db::name('driver')->update($update);
        msg(0, '更改成功', $data['is_online']);
    }

    //获取司机信息
    public function getDriverInfo(){
        msg(0, '查询成功', $this->DriverInfo);
    }

    //接单大厅列表
    public function roomOrderList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', '=', 2];
        $data_count = Db::name('driver_order')
            ->where($where)->count();
        $data_list = Db::name('driver_order')
            ->where($where)
            ->order('pay_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['go_time']);
            $hospital_info = Db::name('hospital')->find($v['hospital_id']);
            $data_list[$k]['hospital_name'] = $hospital_info['name'];
            $server_info = Db::name('server3')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;
            $data_list[$k]['get_money'] = number_format($v['price']*$this->DriverInfo['count'],2);
        }
        msg(0, '查询成功', $data_list,$data_count);
    }
    //接送订单接单
    public function ReceivingServerOrder(){
        $data = $this->request->param();
        $update=array(
            'id'=>$data['id'],
            'status'=>7,
            'dispatch_type'=>1,
            'dispatch_time'=>time(),
            'driver_id'=>$this->DriverId
        );
        Db::startTrans();
        try {
            $update_data=Db::name('driver_order')->update($update);
            Db::commit();
            msg(0, '查询完成',$data['id'],$update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //待处理订单列表
    public function waitOrderList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
       // $where[] = ['order_type', '=', 1];
        $where[] = ['status', '=', 7];
        $where[] = ['driver_id', '=', $this->DriverId];
        $data_list = Db::name('driver_order')
            ->where($where)
            ->order('dispatch_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['go_time']);
            $hospital_info = Db::name('hospital')->find($v['hospital_id']);
            $data_list[$k]['hospital_name'] = $hospital_info['name'];
            $server_info = Db::name('server3')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;
            $data_list[$k]['get_money'] = number_format($v['price']*$this->DriverInfo['count'],2);
        }
        msg(0, '查询成功', $data_list);
    }
    //接送订单员开始处理订单
    public function handleOrder()
    {
        $data = $this->request->param();
        $order_detail=Db::name('driver_order')->find($data['id']);
        $update = array(
            'id'=>$data['id'],
            'status' => 3,
            'start_time' => time(),
            'get_money'=> number_format($order_detail['price']*$this->DriverInfo['count'],2)
        );
        Db::startTrans();
        try {
            $update_data=Db::name('driver_order')->update($update);
            Db::commit();
            msg(0, '查询完成', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //服务中订单列表
    public function  getServerOrderList(){
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', 'in', [3]];
        $where[] = ['driver_id', '=', $this->DriverId];
        $data_count = Db::name('driver_order')
            ->where($where)->count();
        $data_list = Db::name('driver_order')
            ->where($where)
            ->order('start_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['go_time']);
            $data_list[$k]['dispatch_time'] = date('Y-m-d H:i:s', $v['dispatch_time']);
            $data_list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
            $hospital_info = Db::name('hospital')->find($v['hospital_id']);
            $data_list[$k]['hospital_name'] = $hospital_info['name'];
            $server_info = Db::name('server3')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;
        }
        msg(0, '查询完成', $data_list,$data_count);
    }

    //接送订单历史订单
    public function historyServerOrder(){
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', 'in', [4,8]];
        $where[] = ['driver_id', '=', $this->DriverId];
        $data_count = Db::name('driver_order')
            ->where($where)->count();
        $data_list = Db::name('driver_order')
            ->where($where)
            ->order('end_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['go_time']);
            $data_list[$k]['dispatch_time'] = date('Y-m-d H:i:s', $v['dispatch_time']);
            $data_list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
            $data_list[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
            $hospital_info = Db::name('hospital')->find($v['hospital_id']);
            $data_list[$k]['hospital_name'] = $hospital_info['name'];
            $server_info = Db::name('server3')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;
        }
        msg(0, '查询完成', $data_list,$data_count);
    }

    //账户中心顶部
    public function driverFinanceTop(){
        //可提现
        //待入账
        $detail_money=Db::name('driver_order')->where([
            ['driver_id','=',$this->DriverId],
            ['status','in',[3]],
            ['is_del','=',0]
        ])->sum('price');
        $b=number_format($detail_money*(100-$this->DriverInfo['discount'])/100,2);
        //提现中
        $c=Db::name('member_finance')->where([
            ['type','=',5],
            ['is_kuan','=',0],
            ['member_id','=',$this->memberId]
        ])->sum('money');
        //已提现
        $d=Db::name('member_finance')->where([
            ['type','=',5],
            ['is_kuan','=',1],
            ['member_id','=',$this->memberId]
        ])->sum('money');
        $return['a']=$this->DriverInfo['money'];
        $return['b']=$b;
        $return['c']=$c;
        $return['d']=$d;
        msg(0, '查询成功', $return);
    }
    //收支明细
    public function driverSZList(){
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['member_id', '=', $this->memberId];
        //1收入 2支出
        if ($data['type']==1){
            $where[] = ['type', 'in', [2]];
        }elseif ($data['type']==2){
            $where[] = ['type', 'in', [5]];
        }else{
            $where[] = ['type', 'in', [2,5]];
        }
        if (isset($data['date'])&&$data['date']){
            $startOfDay=strtotime($data['date']);
            $endOfDay=strtotime($data['date'].'23:59:59');
            $where[] = ['create_time', 'between', [$startOfDay,$endOfDay]];
        }
        $data_list = Db::name('member_finance')
            ->where($where)
            ->order('create_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k=>$v){
            $data_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        msg(0, '查询成功', $data_list);
    }
    /**
     * 司机提现
     */
    public function driverTixian()
    {
        $data = $this->request->param();
        if ($data['amount'] <= 0) {
            msg(-1, '输入正确值', '');
        }

        if ($data['amount'] > $this->DriverInfo['money']) {
            msg(-1, '提现金额不能大于余额', '');
        }
        Db::startTrans();
        try {
            $insert_data = array(
                'member_id' => $this->memberId,
                'money' => $data['amount'],
                'type' => 5,
                'tixian_type' => $data['tixian_type'], //1银行 2支付宝 3微信
                'tixian_account' => $data['tixian_account'], //提现账号
                'tixian_name' => $data['tixian_name'],//提现人姓名
                'tixian_bank' => $data['tixian_bank'],//提现银行名（银行提现使用）
                'desc' => '司机余额提现',
                'status' => 0,//'0未完成 待审核 1已完成 审核完成 提现使用',
                'create_time' => time(),
                'is_count' => 2
            );
            $insert_id = Db::name('member_finance')->insertGetId($insert_data);
            Db::name('pz')
                ->where('is_del','=',0)
                ->where('id', '=', $this->PzId)
                ->setDec('money', $data['amount']);
            Db::commit();
            msg(0, '更新成功', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //提现记录
    public function drivertixianList(){
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['member_id', '=', $this->memberId];
        $where[] = ['type', '=', 5];
        $data_list = Db::name('member_finance')
            ->where($where)
            ->order('create_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k=>$v){
            $data_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        msg(0, '查询成功', $data_list);
    }
}